package util;

import model.Input;

import org.apache.commons.math3.ode.events.EventHandler;
import org.apache.commons.math3.ode.events.EventHandler.Action;

public class JankenponEventHandler implements EventHandler{

	public Input[] inputs; // the integer is the index in the y state array
	protected double value = 1;
	protected int whoseTurn = 0;
	
	public JankenponEventHandler(Input[] inputs){
		this.inputs = inputs;
		if(inputs!= null && inputs.length < 3){
			whoseTurn = inputs.length-1; // TODO that is a pretty bad hack
		}
	}
	
	//@Override
	public Action eventOccurred(double arg0, double[] arg1, boolean arg2) {
		
		return EventHandler.Action.RESET_STATE;
	}

	//@Override
	public double g(double t, double[] y) {
		
		return ( whoseTurn == (inputs.length)?this.value :value*(this.inputs[whoseTurn].timeStep-t+1));
	}

	//@Override
	public void init(double arg0, double[] arg1, double arg2) {

	}

	//@Override
	public void resetState(double t, double[] y) {

		y[inputs[whoseTurn].receivingSeq] = y[inputs[whoseTurn].receivingSeq] + inputs[whoseTurn].ammount;
				
		whoseTurn++;
		value = - value;
	}

	
}
